package com.forfunnet.minjian.ui;

import android.content.Intent;
import android.widget.EditText;

import com.forfunnet.minjian.R;
import com.forfunnet.minjian.consts.LoginType;
import com.forfunnet.minjian.message.request.LoginRequest;
import com.forfunnet.minjian.message.response.LoginResponse;
import com.forfunnet.minjian.utils.CommonUtils;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

/**
 * Created by RexQian on 4/2/16.
 */
@EActivity(R.layout.activity_login)
public class LoginActivity extends BaseActivity{
    private static final int REQUEST_CODE_REGISTER = 1;

    @Bean
    ActivityHelper mActivityHelper;

    @ViewById(R.id.textAccount)
    EditText mEditAccount;

    @ViewById(R.id.textPassword)
    EditText mEditPassword;

    @Click(R.id.btn_back)
    void onBackButtonClick() {
        setResult(RESULT_CANCELED);
        finish();
    }

    @Click(R.id.btnLogin)
    void login() {
        String account = mEditAccount.getText().toString();
        String password = mEditPassword.getText().toString();
        if (account.isEmpty()) {
            mActivityHelper.showShortToast(getString(R.string.account_empty));
            mEditAccount.requestFocus();
            return;
        }
        if (password.isEmpty()) {
            mActivityHelper.showShortToast(getString(R.string.hint_password));
            mEditPassword.requestFocus();
            return;
        }

        boolean isPhoneNumber = CommonUtils.isValidPhoneNumber(account);

        if (!isPhoneNumber) {
            mActivityHelper.showShortToast(getString(R.string.account_format_error));
            mEditAccount.requestFocus();
            return;
        }

        mActivityHelper.showLoadingDialog(getString(R.string.login_wait));
        loginBackground(account, password);
    }

    @Background
    void loginBackground(String account, String password) {
        LoginRequest request = new LoginRequest();
        request.OS = CommonUtils.getOsName();
        request.UserName = account;
        request.Password = password;
        request.Version = mApp.getVersion();
        LoginResponse resp = mApp.restClient().login(request);
        if (resp == null) {
            onLoginFail(getString(R.string.login_error_network));
        }
        else if (resp.ResponseStatus != null && resp.ResponseStatus.Message != null) {
            String err;
            if (resp.ResponseStatus.Errors != null && resp.ResponseStatus.Errors.size() > 0)
                err = resp.ResponseStatus.Errors.get(0).Message;
            else
                err = resp.ResponseStatus.Message;
            onLoginFail(err);
        }
        else {
            onLoginSuccess(resp, LoginType.NORMAL);
        }
    }


    @UiThread
    void onLoginSuccess(LoginResponse resp, int loginType) {
        setCookie();

        mApp.pref().edit().userId().put(resp.UserId).apply();

        mActivityHelper.dismissLoadingDialog();
        mApp.pref().edit().loginType().put(loginType).apply();

        setResult(RESULT_OK);
        finish();
    }

    @UiThread
    void onLoginFail(String err) {
        mActivityHelper.dismissLoadingDialog();
        mActivityHelper.showShortToast(err);
    }

    void setCookie() {
        mApp.pref().edit().cookie().put(mApp.restClient().getCookie("ss-id")).apply();
    }


    @Click(R.id.btnRegister)
    void register() {
        RegisterActivity_.intent(this).startForResult(REQUEST_CODE_REGISTER);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_REGISTER) {
            if (resultCode == RESULT_OK) {
                setCookie();
                setResult(RESULT_OK);
                finish();
            }
        }
    }

    @Click(R.id.textFindPassword)
    void findPassword() {
        PasswordActivity_.intent(this).start();
    }
}
